package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SetmealServiceimpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithSetmeal(SetmealDto setmealDto) {
        save(setmealDto);
        Long setmealid = setmealDto.getId();

        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list.forEach(x -> x.setSetmealId(setmealid));

        setmealDishService.saveBatch(list);
    }

    @Override
    public void removeWithSetmeal(Long id) {
        removeById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.removeById(queryWrapper);
    }


}
